#include<bits/stdc++.h>
#define int long long
#define N 1000
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define Pr pair<int,int>
#define MP(i,j) make_pair(i,j)
#define Fo(i,j) for(auto i:j)
#define FO(i,j,k) for(int i=j;i;i=k)
#define PB(i) push_back(i)
#define FFF(i,j,k) for(auto i=j;i!=k;++i)
#define For(i,j,k) for(long long i=j;i<=k;++i)
#define FoR(i,j,k) for(long long i=j;i<k;++i)
#define FOR(i,j,k) for(long long i=j;i>=k;--i)
using namespace std;
template <typename _Tp>void read(_Tp& first) {
    _Tp x = 0, f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
    first = x * f;
}
int n,ans,k[N],b[N];
int a[N],vis[N];
vector<int>t[N];
void dfs(int x){
    if(x==n){
        int s=0;
        For(i,1,n)b[i]=a[i];
        For(i,1,n){
            s+=b[k[i]];
            Fo(v,t[k[i]]){
                b[v]+=b[k[i]];
            }
        }
        ans=max(ans,s);
        return ;
    }
    For(i,1,n){
        if(!vis[i]){
            k[x+1]=i;
            vis[i]=1;
            dfs(x+1);
            vis[i]=0;
        }
    }
}
signed main(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
    read(n);
    For(i,1,n)read(a[i]);
    FoR(i,1,n){
        int u,v;
        read(u),read(v);
        t[u].PB(v);
        t[v].PB(u);
    }
    dfs(0);
    cout<<ans;
    return 0;
}
/*
5
1 2 3 4 5
1 2
1 3
2 4
2 5
*/
